/**
 * 
 */
package org.xelnaga.defiler.business.contentlet;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.xelnaga.defiler.business.ContentletAPI;
import org.xelnaga.defiler.dao.support.SearchTemplate;
import org.xelnaga.defiler.domain.ContainerModel;
import org.xelnaga.defiler.domain.ContentContainerModel;
import org.xelnaga.defiler.domain.ContentContainerWrapper;
import org.xelnaga.defiler.domain.ContentletModel;
import org.xelnaga.defiler.domain.ContentletWrapper;
import org.xelnaga.defiler.domain.FieldModel;
import org.xelnaga.defiler.domain.FolderModel;
import org.xelnaga.defiler.service.ContainerService;
import org.xelnaga.defiler.service.ContentContainerService;
import org.xelnaga.defiler.service.ContentletService;
import org.xelnaga.defiler.service.FieldService;
import org.xelnaga.defiler.service.FolderService;
import org.xelnaga.defiler.web.controller.OrderForm;
import org.xelnaga.defiler.web.util.PagerDisplay;
import org.xelnaga.defiler.web.util.PagerDisplayImpl;

/**
 * @author qijb
 *
 */
@Component
public class ContentletAPIImpl implements ContentletAPI {
	@Autowired
	private ContentContainerService contentContainerService;
	@Autowired
	private ContentletService contentletService;
	@Autowired
	private FieldService fieldService;
	@Autowired
	private FolderService folderService;
	@Autowired
	private ContainerService containerService;
	@Override
	public ContentletWrapper getContent(Long inode) {
		ContentletModel contentlet = contentletService.getByPrimaryKey(inode);
		List<FieldModel> fields = fieldService.getByStructureInode(contentlet.getStructureInode());
		return new ContentletWrapper(fields, contentlet);
	}

	@Override
	public List<ContentletWrapper> getListByFolderAndContainer(
			Long folderInode, Long containerInode, Integer count) {
		List<ContentletWrapper> result = new ArrayList<ContentletWrapper>();
		ContainerModel container = containerService.getByPrimaryKey(containerInode);
		if(null != container && null != container.getShowOnMenu() && container.getShowOnMenu()) {
			if(null == count) {
				count = container.getMaxContentlets();
			} else {
				count = count.intValue() > container.getMaxContentlets().intValue() ? container.getMaxContentlets() : count;
			}
			List<FieldModel> fields = fieldService.getByStructureInode(container.getStructureInode());
			List<ContentletModel> contentlets = this.contentletService.getListByFolderContainer(folderInode, container, count);
			for(ContentletModel content : contentlets) {
				result.add(new ContentletWrapper(fields, content));
			}
		}
		return result;
	}

	
	@Override
	public List<ContentContainerWrapper> getListByFolderContainerAndDate(
			Long folderInode, Long containerInode, Integer count) {
		
		List<ContentContainerWrapper> result = new ArrayList<ContentContainerWrapper>();
		ContainerModel container = containerService.getByPrimaryKey(containerInode);
		if(null != container && null != container.getShowOnMenu() && container.getShowOnMenu()) {
			if(null == count) {
				count = container.getMaxContentlets();
			} else {
				count = count.intValue() > container.getMaxContentlets().intValue() ? container.getMaxContentlets() : count;
			}
			List<FieldModel> fields = fieldService.getByStructureInode(container.getStructureInode());
			List<ContentContainerModel> contentlets = contentContainerService.getListByFolderContainerWithinDate(folderInode, container, new Date(), count);
			for(ContentContainerModel content : contentlets) {
				result.add(new ContentContainerWrapper(fields, content));
			}
		}
		return result;
//		List<ContentletWrapper> result = new ArrayList<ContentletWrapper>();
//		ContainerModel container = containerService.getByPrimaryKey(containerInode);
//		if(null != container && null != container.getShowOnMenu() && container.getShowOnMenu()) {
//			if(null == count) {
//				count = container.getMaxContentlets();
//			} else {
//				count = count.intValue() > container.getMaxContentlets().intValue() ? container.getMaxContentlets() : count;
//			}
//			List<FieldModel> fields = fieldService.getByStructureInode(container.getStructureInode());
//			List<ContentletModel> contentlets = contentletService.getListByFolderContainerAndDate(folderInode, container, new Date(), count);
//			for(ContentletModel content : contentlets) {
//				result.add(new ContentletWrapper(fields, content));
//			}
//		}
//		return result;
	}

	@Override
	public List<ContentContainerWrapper> getContentContainerListByContainerInFolder(
			Long folderInode, Long containerInode, Integer count) {
		List<ContentContainerWrapper> result = new ArrayList<ContentContainerWrapper>();
		ContainerModel container = containerService.getByPrimaryKey(containerInode);
		if(null != container && null != container.getShowOnMenu() && container.getShowOnMenu()) {
			if(null == count) {
				count = container.getMaxContentlets();
			} else {
				count = count.intValue() > container.getMaxContentlets().intValue() ? container.getMaxContentlets() : count;
			}
			List<FieldModel> fields = fieldService.getByStructureInode(container.getStructureInode());
			List<ContentContainerModel> contentlets = contentContainerService.getListByFolderContainer(folderInode, container, count);
			for(ContentContainerModel content : contentlets) {
				result.add(new ContentContainerWrapper(fields, content));
			}
		}
		return result;
	}

	@Override
	public ContainerModel getContainer(Long inode) {
		return containerService.getByPrimaryKey(inode);
	}

	@Override
	public ContainerModel getDailyHotContainer() {
		Long inode = Long.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_WEEK));
		ContainerModel model = containerService.getByPrimaryKey(inode);
		if(null == model) {
			model = new ContainerModel();
		}
		return model;
	}

	@Override
	public PagerDisplay<ContentletWrapper> getListByFolder(FolderModel folder,
			Integer start, Integer count) {
		SearchTemplate searchTemplate = new SearchTemplate();
		List<ContentletModel> list = contentletService.getListByFolderWithPager(folder, searchTemplate);
		int totle = contentletService.getCountByFolderWithPager(folder, searchTemplate);
		List<ContentletWrapper> wrapperList = new ArrayList<ContentletWrapper>();
		for(ContentletModel content : list) {
			ContentletWrapper wrapper = new ContentletWrapper(fieldService.getByStructureInode(content.getStructureInode()), content);
			wrapperList.add(wrapper);
		}
		return new PagerDisplayImpl<ContentletWrapper>(wrapperList, start, count, totle);
	}

	@Override
	public List<ContentletWrapper> getListByHotSale(Long contentletInode,
			Long containerInode, Integer count) {
		List<ContentletWrapper> result = new ArrayList<ContentletWrapper>();
		FolderModel folder = folderService.getByContentletInode(contentletInode);
		ContainerModel container = containerService.getByPrimaryKey(containerInode);
		if(null != container && null != container.getShowOnMenu() && container.getShowOnMenu()) {
			if(null == count) {
				count = container.getMaxContentlets();
			} else {
				count = count.intValue() > container.getMaxContentlets().intValue() ? container.getMaxContentlets() : count;
			}
			List<FieldModel> fields = fieldService.getByStructureInode(container.getStructureInode());
			List<ContentletModel> contentlets = contentletService.getListByFolderContainer(folder.getInode(), container, count);
			for(ContentletModel content : contentlets) {
				result.add(new ContentletWrapper(fields, content));
			}
		}
		return result;
	}

	@Override
	public List<ContentletWrapper> getListByFaverite(Long contentletInode, Integer count) {
		List<ContentletWrapper> result = new ArrayList<ContentletWrapper>();
		//TODO
		return result;
	}

	@Override
	public List<ContentletWrapper> getListByFolderFriend(Long contentletInode, Integer count) {
		List<ContentletWrapper> result = new ArrayList<ContentletWrapper>();
		FolderModel folder = folderService.getByContentletInode(contentletInode);
		ContentletModel contentlet = contentletService.getByPrimaryKey(contentletInode);
		List<FieldModel> fields = fieldService.getByStructureInode(contentlet.getStructureInode());
		List<ContentletModel> contentlets = contentletService.getListByFolderFriend(folder.getInode(), count);
		for(ContentletModel content : contentlets) {
			//only show firend except itself.
			if(content.getInode().longValue() != contentletInode.longValue()) {
				result.add(new ContentletWrapper(fields, content));
			}
		}
		return result;
	}

	@Override
	public List<ContentletWrapper> getListByItemBasedRecommand(
			Long contentletInode, Integer count) {
		//TODO 
		return new ArrayList<ContentletWrapper>();
	}

	@Override
	public OrderForm getUserCart(String cookieValue) {
		// TODO Auto-generated method stub
		return null;
	}
	
	
}
